import Vue from 'vue';
import Vuex from 'vuex';
import router from '../router/router.js'
import userApi from '@/api/user/userApi'
import menuRoleApi from '@/api/menuRole/menuRoleApi'
import {parsePermList} from '@/js/utils/permUtils'
import {parseRouterList} from '@/js/utils/routerUtils'
import de from "element-ui/src/locale/lang/de";


Vue.use(Vuex);

/**
 * Vuex全局状态管理
 * @param options {Array} 用于渲染tabs的数组
 */
const store = new Vuex.Store({
    state: {
        openList: [],
        activePage: '/index',
        cmsUser: null,          //用户信息
        cmsRole: null,            //权限信息
    //    cmsRouter: null,         //路由信息
        cmsMenu: new Array(),  //菜单信息
        isFirst: true,           //标志是第一次访问，还是刷新页面
    },
    getters: {
        getOpenList(state){
            return state.openList
        },
        getCmsUser(state){
            return state.cmsUser
        },
        getCmsRole(state){
            return state.cmsRole
        },
        // getCmsRouter(state) {
        //     return state.myRouter
        // },
        getCmsMenu(state){
            return state.cmsMenu
        },
        getIsFirst(state) {
            return state.isFirst
        },
    },
    mutations: {
        SET_CMS_USER(state,cmsUser){
            state.cmsUser = cmsUser
        },
        SET_CMS_ROLE(state,cmsRole){
            state.cmsRole = cmsRole
        },
        // SET_CMS_ROUTER(state, router) {
        //     state.myRouter = router;
        // },
        SET_CMS_MENU(state,cmsMenu){
            state.cmsMenu = cmsMenu
        },
        SET_IS_FIRST(state, isFirst) {
            state.isFirst = isFirst
        },
        CLEAR_USER(state) {
            state.cmsUser = null
            state.cmsRole = null
            state.cmsMenu = new Array()
        },
        addOpenList(state, route){
            let isActive=false;
            if (state.openList.length==0){  //当openList没有数据时，默认添加首页
                state.openList.push({'name':'首页','path':'/index'})
            }
            for (let item in state.openList){   //将当前路由和openList比较，
                if (state.openList[item].path==route.path){
                    isActive=true;
                }
            }
            if (isActive){  //如果在里面已经存在，则只需要激活就可以了
                state.activePage=route.path
            }else{  //如果不存在就添加当前路由，并且激活
                state.openList.push({'name':route.name,'path':route.path})
                state.activePage=route.path
            }
        },
        delOpenList(state,route){
            if (route!='/index'){   //首页不可删除
                let index;
                for (let item in state.openList){   //找出当前要删除的路由
                    if (state.openList[item].path==route){
                        index=item;
                        break;
                    }
                }
                state.openList.splice(index,1)     //执行删除
                if (route==state.activePage){   //如果当前删除的路是当前激活的路由，则上一个路由被激活
                    router.push(state.openList[index-1].path)
                    state.activePage=state.openList[index-1].path
                }
            }

        },
        updOpenList(state,route){  //点击切换tabs
            state.activePage=route.name
        }
    },
    actions:{
        async SetCmsUser({commit}, state) {
            let res = await userApi.getUser(JSON.parse(localStorage.getItem("cmsUser")).id);
            commit('SET_CMS_USER', res.data)
        },
        async SetCmsMenu({commit}, state) {
            let res = await menuRoleApi.getList3(JSON.parse(localStorage.getItem("cmsUser")).userRole)
            commit('SET_CMS_MENU', res.data)
        },
        SetCmsRole({commit}, state) {
            let roleList = parsePermList(new Array(),this.state.cmsMenu)
            commit('SET_CMS_ROLE',roleList )
        },
        // SetCmsRouter({commit}, state) {
        //     let routerList = parseRouterList(this.state.cmsMenu)
        //     commit('SET_CMS_ROUTER',routerList)
        // },
        SetIsFirst({ commit}){
            commit('SET_IS_FIRST', false)
        }
    }
});

export default store;
